﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NVSecurity.Models;
using NVSecurity.Utility;

namespace NVSecurity.Areas.Admin.Controllers
{
    public class CustomerController : Controller
    {
        private NVSecurityEntities db = new NVSecurityEntities();

        //
        // GET: /Admin/Customer/

        public ActionResult Index()
        {
            return View(db.Customers.ToList());
        }

        //
        // GET: /Admin/Customer/Details/5

        public ActionResult Details(int id = 0)
        {
            Customer customer = db.Customers.Find(id);
            if (customer == null)
            {
                return HttpNotFound();
            }
            return View(customer);
        }

        //
        // GET: /Admin/Customer/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Admin/Customer/Create

        [HttpPost]
        public ActionResult Create(Customer customer)
        {
            if (ModelState.IsValid)
            {
                String NamePro = null;

                if (Request.Files["img1"] != null)
                {
                    HttpPostedFileBase file0 = Request.Files["img1"];
                    string fullname = Request.Files["img1"].FileName;
                    if (fullname != "")
                    {
                        NamePro = fullname.Substring(1 + fullname.LastIndexOf("\\"));
                        string rename = HelperSEO.ToSeoImage(customer.CustomerName + DateTime.Now.Ticks + NamePro.Substring(NamePro.LastIndexOf(".")));
                        Utility.ResizeImage.Resize(Server.MapPath("~/Images/Customer/"), rename, 400, 400, file0.InputStream);
                        customer.ImagePath = rename;
                    }
                }
                db.Customers.Add(customer);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(customer);
        }

        //
        // GET: /Admin/Customer/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Customer customer = db.Customers.Find(id);
            if (customer == null)
            {
                return HttpNotFound();
            }
            return View(customer);
        }

        //
        // POST: /Admin/Customer/Edit/5

        [HttpPost]
        public ActionResult Edit(Customer customer)
        {
            if (ModelState.IsValid)
            {
                String NamePro = null;

                if (Request.Files["img1"] != null)
                {
                    HttpPostedFileBase file0 = Request.Files["img1"];
                    string fullname = Request.Files["img1"].FileName;
                    if (fullname != "")
                    {
                        NamePro = fullname.Substring(1 + fullname.LastIndexOf("\\"));
                        string rename = HelperSEO.ToSeoImage(customer.CustomerName + DateTime.Now.Ticks + NamePro.Substring(NamePro.LastIndexOf(".")));
                        Utility.ResizeImage.Resize(Server.MapPath("~/Images/Customer/"), rename, 400, 400, file0.InputStream);
                        customer.ImagePath = rename;
                    }
                }
                db.Entry(customer).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(customer);
        }

        //
        // GET: /Admin/Customer/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Customer customer = db.Customers.Find(id);
            if (customer == null)
            {
                return HttpNotFound();
            }
            return View(customer);
        }

        //
        // POST: /Admin/Customer/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Customer customer = db.Customers.Find(id);
            db.Customers.Remove(customer);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}